import { resultSuce, resultErr, getRequestToken, requestParams } from './_utils'
import Mock from 'mockjs';
import { faker } from '@faker-js/faker';
import {formatToDate} from '@/utils/dateUtil'

// 模拟账号信息
function createFakeUserList() {
  return [
    {
      userId: '1',
      userName: 'admin',
      realName: 'sssgoEasy Admin',
      avatar: '',
      desc: 'manager',
      password: '123456',
      token: 'fakeToken1',
      auths: ['*:*:*'],
      modules: [],
      is_admin: 1,
      role_name: '管理员角色',
      mobile: 13000000000,
      last_login: '2021-11-11 12:00',
      role: 1, // 管理
    },
    {
      userId: '2',
      userName: 'test',
      password: '123456',
      realName: 'test user',
      avatar: '',
      desc: 'tester',
      token: 'fakeToken2',
      auths: ['app:user:add', 'app:user:modify'],
      modules: ['home', 'component', 'user'],
      is_admin: 0,
      role_name: '普通用户角色',
      mobile: 18000000000,
      last_login: '2021-11-11 12:12',
      role: 0, // 普通
    },
  ]
}

enum URL {
  login = '/mock/login',
  permission = '/mock/permission',
  userList = '/mock/user/userList',
  addUser = '/mock/user/addUser',
  updateUser = '/v1/user/updateUser',
  deletUser = '/v1/user/deletUser',
}

const userList = Mock.mock({
  'items|30': [
    {
      'id|+1': 1,
      title: '@ctitle',
      mobile: () => faker.phone.number('18#########'),
      nickName: '@cname',
      userName: () => 'H' + faker.random.numeric(5),
      description: '@cparagraph',
      createdAt: '@datetime',
      updatedAt: '@datetime',
      age: '@natural(20,50)',
      'role|1': [0, 1],
      email: '@email',
    },
  ],
});

export default [
  {
    url: '/mock/login',
    timeout: 200,
    method: 'post',
    response: ({ body }) => {
      const { userName, password } = body
      const checkUser = createFakeUserList().find(
        (item) => item.userName === userName && password === item.password,
      )
      delete checkUser?.password
      if (checkUser) return resultSuce(checkUser)
      return resultErr('用户名或密码错误')
    },
  },
  {
    url: URL.permission,
    method: 'get',
    response: (request: requestParams) => {
      const token = getRequestToken(request);
      if (!token) return resultErr('Invalid token');
      const checkUser = createFakeUserList().find((item) => item.token === token);
      if (!checkUser) {
        return resultErr('The corresponding user information was not obtained!');
      }
      return resultSuce(checkUser);
    },
  },
  {
    // url: URL.userList,
    url: RegExp(URL.userList+'.*'),
    timeout: 200,
    method: 'get',
    response: (request) => {
      const token = getRequestToken(request)
      if (!token) return resultErr('登陆状态失效，请重新登陆')
      const { pageSize, pageNum } = request.query
      
      const items = pageSize && pageNum ?  userList.items.slice((pageNum - 1) * pageSize, pageSize*pageNum) : userList.items;
      // console.log(items);
      
      return resultSuce({
        total: userList.items.length,
        list: items,
      })
    },
  },
  {
    url: URL.addUser,
    method: 'post',
    response: (request) => {
      const token = getRequestToken(request)
      if (!token) return resultErr('登陆状态失效，请重新登陆')
      const { nickName, mobile, email, role } = request.body;
      userList.items.unshift(
        Mock.mock({
          id: userList.items.length + 1,
          title: '@ctitle',
          mobile,
          nickName,
          userName: () => 'H' + faker.random.numeric(5),
          description: '@cparagraph',
          createdAt: formatToDate(new Date()),
          updatedAt: formatToDate(new Date()),
          age: '@natural(20,50)',
          role: role,
          email,
        }),
      );
      return resultSuce({ nickName, mobile, email, role })
    },
  },
]
